package jys.test.yddata.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.lang.Math;

public class DrawLevelView extends View implements SensorEventListener {
    private int line_r = Color.parseColor("#FF0000");
    private int line_g = Color.parseColor("#00FF00");
    private SensorManager sensorManager;
    private Sensor sensor;
    private double up8down, left8right;     // 8 : &
    private final static int epsilon = 3;   // 角度接收范围

    public DrawLevelView(Context context) {
        super(context);
        initSensor();
    }

    public DrawLevelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initSensor();
    }

    public DrawLevelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSensor();
    }

    private void initSensor() {
        sensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public boolean sensorIsEnable() {
        return sensor == null;
    }

    @Override
    @SuppressLint("DrawAllocation")
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setStrokeWidth(2);
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);
        paint.setTextSize(30);


        int wid = getWidth()/2,
                hei = getHeight()/2;
        int len = wid + hei;
        float height = (float) (hei*up8down/90);

        canvas.translate(wid, hei);
        canvas.rotate((float) left8right);

        // 垂直刻度线
        paint.setColor(Math.abs(left8right)<epsilon? line_g:line_r);
        canvas.drawCircle(0, 0, 20, paint);
        canvas.drawLine(0, -len, 0, len, paint);
        canvas.drawLine(-20, hei, 20, hei, paint);
        canvas.drawLine(-20, -hei, 20, -hei, paint);
        canvas.drawLine(-20, hei/2, 20, hei/2, paint);
        canvas.drawLine(-20, -hei/2, 20, -hei/2, paint);
        canvas.drawText((int)left8right+"°", 30, -5, paint);

        // 水平角度线
        paint.setTextAlign(Paint.Align.RIGHT);
        paint.setColor(Math.abs(up8down)<epsilon? line_g:line_r);
        canvas.drawLine(-len, height, len, height, paint);
        canvas.drawText((int)up8down+"°", -30, height-5, paint);

// 没使用translate和rotate的沙比代码纪念
//        double pil8r1 = -Math.toRadians(left8right),
//               pil8r2 = -Math.toRadians(left8right+90);
//        double sinyi1 = Math.sin(pil8r1),
//               cosyi1 = Math.cos(pil8r1);
//        int siny1 = (int) (sinyi1*len),
//            cosy1 = (int) (cosyi1*len),
//            siny2 = (int) (Math.sin(pil8r2)*len),
//            cosy2 = (int) (Math.cos(pil8r2)*len);
//        int kx = (int) (sinyi1*wid),
//            ky = (int) (cosyi1*wid);
//        int vx = (int) (sinyi1*wid*up8down/90),
//            vy = (int) (cosyi1*wid*up8down/90);
//
//        // 水平角度线
//        paint.setColor(Math.abs(up8down)<epsilon? line_g:line_r);
//        canvas.drawLine(-siny2+wid+vx, -cosy2+hei+vy, siny2+wid+vx, cosy2+hei+vy, paint);
//
//        // 垂直刻度线
//        paint.setColor(Math.abs(left8right)<epsilon? line_g:line_r);
//        canvas.drawCircle(wid, hei, 20, paint);
//        canvas.drawLine(-siny1+wid, -cosy1+hei, siny1+wid, cosy1+hei, paint);
//        canvas.drawLine(-siny2*20/len+wid-kx, -cosy2*20/len+hei-ky, siny2*20/len+wid-kx, cosy2*20/len+hei-ky, paint);
//        canvas.drawLine(-siny2*20/len+wid+kx, -cosy2*20/len+hei+ky, siny2*20/len+wid+kx, cosy2*20/len+hei+ky, paint);
//        canvas.drawLine(-siny2*10/len+wid-kx/2, -cosy2*10/len+hei-ky/2, siny2*10/len+wid-kx/2, cosy2*10/len+hei-ky/2, paint);
//        canvas.drawLine(-siny2*10/len+wid+kx/2, -cosy2*10/len+hei+ky/2, siny2*10/len+wid+kx/2, cosy2*10/len+hei+ky/2, paint);
//        canvas.drawText(""+left8right, wid, hei, paint);

        //每隔10毫秒界面刷新
        postInvalidateDelayed(10);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            up8down = Math.min(Math.max(-sensorEvent.values[1]-90, -90), 90);    // 上下
            left8right = sensorEvent.values[2];    // 左右
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }
}
